Passed
Push — master ( 2b5061...19ac1b )
by Joe Nilson
02:32
created

nessDocViewModalSave   A

Complexity

Conditions 1

Size

Total Lines 20
Code Lines 18

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
dl 20
loc 20
rs 9.5
c 0
b 0
f 0
1
/*
2
 * Copyright (C) 2021 Joe Nilson <[email protected]>
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as
6
 * published by the Free Software Foundation, either version 3 of the
7
 * License, or (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 * You should have received a copy of the GNU Lesser General Public License
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 */
16
17
/**
18
 * @param {string} tipoComprobante
19
 * @returns {Promise<*>}
20
 */
21
async function verificarCorrelativoNCF(tipoComprobante, tipoOperacion)
22
{
23
    let ArrayTipoNCFCompras = ['11','12','16','17'];
24
    if (tipoOperacion === 'Compras' && !ArrayTipoNCFCompras.includes($("#doc_codsubtipodoc").val())) {
25
        return true;
26
    }
27
28
    return $.ajax({
29
        url: 'ListNCFRango',
30
        async: true,
31
        data: {'action': 'busca_correlativo', 'tipocomprobante': tipoComprobante},
32
        type: 'POST',
33
        datatype: 'json',
34
        success: function (response) {
35
            let data = JSON.parse(response);
36
            if ( data.existe === false ) {
37
                executeModal(
38
                    'verificaNCF',
39
                    'No hay Correlativo de NCF Disponible',
40
                    'No hay correlativos disponibles para el Tipo de NCF ' +
41
                    tipoComprobante + ' <br/>Por favor revise su maestro de NCFs',
42
                    'warning'
43
                );
44
            }
45
        },
46
        failure: function (response) {
0 ignored issues
show
Unused Code introduced by
The parameter response is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
47
            alert('Ha ocurrido algún tipo de falla ' + status);
0 ignored issues
show
Bug introduced by
The variable status seems to be never declared. If this is a global, consider adding a /** global: status */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
48
        },
49
        error: function (xhr, status) {
50
            alert('Ha ocurrido algún tipo de error ' + status);
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
51
        }
52
    });
53
}
54
55
/**
56
 * logConsole in Debug mode
57
 * @param value
58
 * @param description
59
 */
60
function logConsole(value, description ='data')
61
{
62
    if ($(".debugbar") !== undefined) {
63
        console.log(description, value);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
64
    }
65
}
66
67
/********
68
 * Util Functions
69
 */
70
71
/**
72
 *
73
 * @param {object} btn
74
 * @param {string} text
75
 */
76
function setLoadingButton(btn, text)
77
{
78
    $(btn).prop("disabled", true);
79
    $(btn).html(
80
        '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>' + text
81
    );
82
}
83
84 View Code Duplication
function setBusinessDocViewModalSave(readOnlySelects, selectOptionsPagos,selectOptionsMovimientos)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
85
{
86
    let message = '<div class="form-content">\n' +
87
        '      <form class="form" role="form">\n' +
88
        '        <div class="form-group">\n' +
89
        '          <label for="ncftipopago">Tipo de Pago</label>\n' +
90
        '          <select class="custom-select" id="ncftipopago" name="ncftipopago"'+readOnlySelects+'>\n' +
91
        selectOptionsPagos +
92
        '          </select>\n' +
93
        '        </div>\n' +
94
        '        <div class="form-group">\n' +
95
        '          <label for="ncftipomovimiento">Tipo de Movimiento</label>\n' +
96
        '          <select class="custom-select" id="ncftipomovimiento" name="ncftipomovimiento"'+readOnlySelects+'>\n' +
97
        selectOptionsMovimientos +
98
        '          </select>\n' +
99
        '        </div>\n' +
100
        '      </form>\n' +
101
        '    </div>';
102
    return message;
103
}
104
105
106
function saveBussinessDocument(btn)
107
{
108
    //Set the save button as loading
109
    setLoadingButton(btn,'Guardando...');
110
111
    var data = {};
112
    $.each($("#" + businessDocViewFormName).serializeArray(), function (key, value) {
0 ignored issues
show
Bug introduced by
The variable businessDocViewFormName seems to be never declared. If this is a global, consider adding a /** global: businessDocViewFormName */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
113
        data[value.name] = value.value;
114
    });
115
    data['ncftipopago'] = $('form #ncftipopago').val();
116
    data['ncftipomovimiento'] = $('form #ncftipomovimiento').val();
117
    data.action = "save-document";
118
    data.lines = getGridData();
119
120
    $.ajax({
121
        type: "POST",
122
        url: businessDocViewUrl,
0 ignored issues
show
Bug introduced by
The variable businessDocViewUrl seems to be never declared. If this is a global, consider adding a /** global: businessDocViewUrl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
123
        dataType: "text",
124
        data: data,
125
        success: function (results) {
126
            if (results.substring(0, 3) === "OK:") {
127
                $("#" + businessDocViewFormName + " :input[name=\"action\"]").val('save-ok');
0 ignored issues
show
Bug introduced by
The variable businessDocViewFormName seems to be never declared. If this is a global, consider adding a /** global: businessDocViewFormName */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
128
                $("#" + businessDocViewFormName).attr('action', results.substring(3)).submit();
129
            } else {
130
                alert(results);
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
131
                $("#" + businessDocViewFormName + " :input[name=\"multireqtoken\"]").val(randomString(20));
132
            }
133
        },
134
        error: function (msg) {
135
            alert(msg.status + " " + msg.responseText);
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
136
        }
137
    });
138
}
139
140
function isBusinessDocumentPage()
141
{
142
    let businessDocument = '';
143
    if ($('#formEditFacturaProveedor').length > 0) {
144
        businessDocument = 'Compra';
145
    } else if ($('#formEditFacturaCliente').length > 0) {
146
        businessDocument = 'Venta';
147
    }
148
    return businessDocument;
149
}
150
151
152
$(document).ready(function () {
153
    let tipoOperacion = isBusinessDocumentPage();
154
    let varCodSubtipoDoc = $("#doc_codsubtipodoc");
155
    if (varCodSubtipoDoc.val() !== '' && tipoOperacion !== '') {
156
        verificarCorrelativoNCF($("#doc_codsubtipodoc").val(), tipoOperacion);
157
    }
158
159
    varCodSubtipoDoc.change(function () {
160
        logConsole(varCodSubtipoDoc.val(),"#doc_codsubtipodoc val");
161
        if (tipoOperacion !== '') {
162
            verificarCorrelativoNCF(varCodSubtipoDoc.val(), tipoOperacion);
163
        }
164
    });
165
});